package com.endurance.dao.impl;

import com.endurance.dao.ProductDao;
import com.endurance.mapper.ProductMapper;
import com.endurance.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProductDaoImpl implements ProductDao {


    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    ProductMapper productMapper;

    @Override
    public void updateProduct(Product product) {
        String sql = "update product SET pname = ?, price = ?, category = ? where pid = ?";
        jdbcTemplate.update(sql, product.getPname(), product.getPrice(), product.getCategory(), product.getPid());

    }

    @Override
    public List<Product> findAll() {
        String sql = "select * from product";
         return jdbcTemplate.query(sql, productMapper);
    }

    @Override
    public void saveProduct(Product product) {
        String sql = "insert into product(pname, price, category) VALUE(?,?,?)";
        jdbcTemplate.update(sql, product.getPname(), product.getPrice(), product.getCategory());
    }

    @Override
    public void deleteProduct(Integer id) {
        String sql = "delete from product where pid = ?";
        jdbcTemplate.update(sql, id);
    }

    @Override
    public Product findById(Integer id) {
        String sql = "select * from product where pid = ?";
        return jdbcTemplate.queryForObject(sql, productMapper);
    }
}
